package com.qixiaobao.web.controller.addminus;

import com.qixiaobao.addminus.domain.Addminus;
import com.qixiaobao.addminus.domain.dto.AddminusDTO;
import com.qixiaobao.addminus.domain.dto.AddminusPreservationNoTempDTO;
import com.qixiaobao.addminus.service.IAddMinusService;
import com.qixiaobao.common.annotation.Log;
import com.qixiaobao.common.annotation.RepeatSubmit;
import com.qixiaobao.common.core.domain.AjaxResult;
import com.qixiaobao.common.core.page.TableDataInfo;
import com.qixiaobao.common.enums.BusinessType;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.common.utils.easyExcel.EasyExcelUtil;
import com.qixiaobao.system.domain.dto.PersonDTO;
import com.qixiaobao.web.controller.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 增减员记录
 * @Author yangxm
 * @Date 2025/6/23 9:56
 */
@RestController
@RequestMapping("/addminus")
public class AddminusController extends BaseController {

    @Autowired
    private IAddMinusService addMinusService;


    /**
     * @Description 增减员列表
     * @Author yangxm
     * @Date 2025/6/23 10:16
     * @param addminusDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/list")
    public TableDataInfo list(AddminusDTO addminusDTO){
        addminusDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        List<Addminus> list = addMinusService.selectAddminusList(addminusDTO);
        return getDataTable(list);
    }

    /**
     * @Description 投保人数总计、增员人数总计、减员人数总计
     * @Author yangxm
     * @Date 2025/6/23 13:58
     * @param addminusDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/addMinusPersonTotal")
    public AjaxResult addMinusPersonTotal(AddminusDTO addminusDTO){
        addminusDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(addMinusService.selectAddMinusPersonTotal(addminusDTO));
    }

    /**
     * @Description 保险费总计
     * @Author yangxm
     * @Date 2025/6/23 14:01
     * @param addminusDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/copeInsureFeeTotal")
    public AjaxResult copeInsureFeeTotal(AddminusDTO addminusDTO){
        addminusDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(addMinusService.selectCopeInsureFeeTotal(addminusDTO));
    }

    /**
     * @Description 人员详情
     * @Author yangxm
     * @Date 2025/6/23 14:01
     * @param personDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/person/list")
    public AjaxResult personList(PersonDTO personDTO){
        startPage();
        return success(addMinusService.personList(personDTO));
    }

    /**
     * @param addminusDTO
     * @param response
     * @return com.qixiaobao.common.core.domain.AjaxResult
     * @Description 导出
     * @Author yangxm
     * @Date 2025/6/23 14:01
     */
    @Log(title = "增减员记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(AddminusDTO addminusDTO, HttpServletResponse response){
        addminusDTO.setNoPermissionSupplyIdList(checkPermission());
        addMinusService.export(addminusDTO, response);
    }

    /**
     * @Description 上传批单
     * @Author yangxm
     * @Date 2025/6/23 15:39
     * @param files 所有文件名以保全单号命名
     * @param addminusId  若不为空，则是 列表页面操作中的 上传批单按钮。若为空，则是 页面更多 中的 批量上传按钮
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "增减员记录", businessType = BusinessType.UPLOAD_ENDORSE)
    @PostMapping("/endorses/upload")
    public AjaxResult uploadEndorses(MultipartFile[] files, Long addminusId){
        if (files == null || files.length == 0){
            throw new UtilException("文件不能为空");
        }
        return AjaxResult.success("操作成功", addMinusService.uploadEndorses(files, addminusId));
    }

    /**
     * @Description 下载定向模板
     * @Author yangxm
     * @Date 2025/6/23 16:51
     * @param response 
     */
    @GetMapping("/endorses/temp/downloadTemp")
    public void downloadEndorsesTemp(HttpServletResponse response){
        List<AddminusPreservationNoTempDTO> list = new ArrayList<>();
        try {
            EasyExcelUtil.writeExcel(response, list, AddminusPreservationNoTempDTO.class, "批单定向下载模板", "sheet1");
        } catch (IOException e) {
            throw new RuntimeException("模板下载失败：" + e.getMessage());
        }
    }
    

    /**
     * @Description 定向下载模板  解析模板内容
     * @Author yangxm
     * @Date 2025/6/23 16:21
     * @param file
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @PostMapping("/endorses/temp/analysisFile")
    public AjaxResult analysisEndorsesFile(MultipartFile file){
        if (file == null){
            throw new UtilException("文件不能为空");
        }
        byte[] bytes;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            throw new UtilException("文件格式转换异常：" + e.getMessage());
        }
        InputStream inputStream = new ByteArrayInputStream(bytes);
        //获取读取excel的数据
        List<AddminusPreservationNoTempDTO> addminusPreservationNoTempDTOList = EasyExcelUtil.readExcel(inputStream, AddminusPreservationNoTempDTO.class);
        if (StringUtils.isEmpty(addminusPreservationNoTempDTOList)){
            throw new UtilException("导入数据不能为空");
        }
        return AjaxResult.success(addMinusService.analysisEndorsesFile(addminusPreservationNoTempDTOList));
    }

    /**
     * @Description 定向下载模板  批量下载批单
     * @Author yangxm
     * @Date 2025/6/23 16:56
     * @param addminusPreservationNoTempDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/endorses/temp/download")
    public AjaxResult downloadEndorsesByPreservationNoTemp(AddminusPreservationNoTempDTO addminusPreservationNoTempDTO) {
        addMinusService.downloadEndorsesByPreservationNoTemp(addminusPreservationNoTempDTO);
        return success();
    }
    
    /**
     * @Description 批量删除批单
     * @Author yangxm
     * @Date 2025/6/23 17:06
     * @param addminusIdArray 
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @DeleteMapping("/endorses/{addminusIdArray}")
    public AjaxResult deleteEndorses(@PathVariable Long[] addminusIdArray) {
        return toAjax(addMinusService.deleteEndorses(addminusIdArray));
    }
}
